package com.zhc.dejingapi.common.utils;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author zhc
 * @date 2024年03月16日 14:13
 */

@Configuration
public class SerialNumUtil {

    @Resource
    private DateTimeUtil dateTimeUtil;

    @Resource(name = "redisTemplateLinux")
    private RedisTemplate<String,Object> redisTemplateLinux;

    public String GenerateSerialNum(String prefix){

        String date = dateTimeUtil.CurrentDateYYYYMMDD();
        long timeOut = dateTimeUtil.TillTheEndOfTodayWithSeconds();
        Boolean b = redisTemplateLinux.hasKey(prefix);
        String num="";
        if(b){
             String redisNum = (String) redisTemplateLinux.opsForValue().get(prefix);
             num = String.format("%04d",Integer.parseInt(redisNum)+1);
        }else{
            num = String.format("%04d",1);
        }
        redisTemplateLinux.opsForValue().set(prefix,num,timeOut,TimeUnit.SECONDS);
        return prefix+date+num;
    }
}
